<template>
  <div class="products-container">
    <products-header></products-header>
    <div class="products-holder">
      <div v-if="products.length > 0" v-for="product in products" :key="product.id">
        <el-card v-if="product.name !== ''" class="product-holder" :body-style="{padding: '0px'}">
          <div slot="header" class="clearfix">
            <span style="float: left;">{{ product.name }}</span>
            <el-button style="float: right; padding: 3px" type="danger" icon="el-icon-minus"></el-button>
            <el-button style="float: right; padding: 3px" type="primary" icon="el-icon-edit"></el-button>
          </div>
          <img v-if="product.haspicture === 1" class="product-img" :src="productPictureUrl + product.id">
          <div v-if="product.haspicture === 0" class="product-add">
            <i class="el-icon-upload2" @click="openAddProductPictureForm(product)"></i>
          </div>
        </el-card>
        <el-card v-if="product.name === ''" class="product-holder" :body-style="{padding: '0px'}">
          <div slot="header" class="clearfix">
            <span>新增商品</span>
          </div>
          <div class="product-add">
            <i class="el-icon-plus" @click="addProductFormVisible = true"></i>
          </div>
        </el-card>
      </div>
      <div v-if="products.length <= 0">
        <el-card class="product-holder" :body-style="{padding: '0px'}">
          <div slot="header" class="clearfix">
            <span>新增商品</span>
          </div>
          <div class="product-add">
            <i class="el-icon-plus" @click="addProductFormVisible = true"></i>
          </div>
        </el-card>
      </div>
    </div>

    <el-dialog width="800px" title="新增商品" :visible.sync="addProductFormVisible">
      <el-form :model="addProductForm">
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="addProductForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" :label-width="formLabelWidth">
          <el-input v-model="addProductForm.price" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品描述" :label-width="formLabelWidth">
          <el-input v-model="addProductForm.description" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品库存" :label-width="formLabelWidth">
          <el-input v-model="addProductForm.restAmount" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addProductFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="this.addProduct">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog width="800px" title="新增商品图片" :visible.sync="addProductPictureFormVisible">
      <el-upload class="product-picture-uploader" :action="addProductPictureUrl" :on-success="handleProductPictureAddSuccess">
        <img v-if="addedProductPictureUrl" :src="addedProductPictureUrl" class="product-picture">
        <i class="el-icon-plus product-picture-uploader-icon"></i>
      </el-upload>

      <div slot="footer" class="dialog-footer">
        <el-button @click="addProductPictureFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addProductPictureFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import ProductsHeader from 'components/Header.vue'
import productService from 'services/productService.js'
import { FileApi } from 'services/apiConfig.js'
import * as notifyUtil from 'utils/notifyUtil.js'

export default {
  components: {
    'products-header': ProductsHeader
  },
  data() {
    return {
      products: [],
      formLabelWidth: '120px',
      addProductFormVisible: false,
      addProductPictureFormVisible: false,
      addProductPictureUrl: '',
      selectedProduct: null,
      addedProductPictureUrl: '',
      productPictureUrl: FileApi.product.getUrl,
      addProductForm: {
        name: '',
        price: '',
        description: '',
        restAmount: ''
      }
    }
  },
  computed: mapGetters({
    nowShop: 'nowShop'
  }),
  filters: {},
  methods: {
    ...mapActions({}),
    getProductsByShopId() {
      productService.getProductsByShopId(this.nowShop.id).then(response => {
        if (
          response.data.Product === null ||
          response.data.Product === undefined
        ) {
          notifyUtil.showError('查找商品失败', '暂未添加商品')
        } else {
          let products = response.data.Product
          products.push({
            id: '',
            name: ''
          })
          this.$set(this, 'products', products)
        }
      })
    },
    addProduct() {
      let productAdded = {
        shopid: this.nowShop.id,
        name: this.addProductForm.name,
        price: parseFloat(this.addProductForm.price),
        description: this.addProductForm.description,
        restamount: parseInt(this.addProductForm.restAmount),
        sellamount: 0,
        haspicture: 0
      }
      productService.addProduct(productAdded).then(response => {
        console.log(response)
        console.log(response.data)
        this.$set(this, 'addProductFormVisible', false)
        if (response.data.id === null || response.data.id === undefined) {
          notifyUtil.showError('添加商品失败', '服务器错误')
        } else {
          this.getProductsByShopId()
        }
      })
    },
    updateProductPicture(oldProduct) {
      let productUpdated = {
        id: oldProduct.id,
        shopid: oldProduct.shopid,
        name: oldProduct.name,
        price: oldProduct.price,
        description: oldProduct.description,
        restamount: oldProduct.restamount,
        sellamount: oldProduct.sellamount,
        haspicture: 1
      }
      productService.updateProduct(productUpdated).then(response => {
        // console.log(response)
        // console.log(response.data)
        // this.$set(this, 'addProductPictureFormVisible', false)
        if (response.data.id === null || response.data.id === undefined) {
          notifyUtil.showError('添加商品图片失败', '服务器错误')
        } else {
          this.getProductsByShopId()
        }
      })
    },
    openAddProductPictureForm(product) {
      this.$set(this, 'addProductPictureFormVisible', true)
      this.$set(this, 'selectedProduct', product)
      this.$set(
        this,
        'addProductPictureUrl',
        FileApi.product.postUrl + product.id
      )
    },
    handleProductPictureAddSuccess() {
      this.updateProductPicture(this.selectedProduct)
      this.$set(
        this,
        'addedProductPictureUrl',
        FileApi.product.getUrl + this.selectedProduct.id
      )
    }
  },
  mounted: function() {
    this.getProductsByShopId()
  }
}
</script>
<style scoped>
.products-container {
  position: absolute;
  width: 100%;
  height: 100%;
  bottom: 0;
  background-color: #F7F7F7;
}
.products-holder {
  /* margin-top: 10px; */
  width: 1200px;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  justify-content: space-between;
  /* align-content: space-between; */
  flex-flow: row wrap;
}
.product-holder {
  /* float: left; */
  width: 200px;
  height: 258px;
  margin-top: 20px;
  /* margin-right: 50px; */
}
.products-holder:after {
  content: '';
  width: 700px;
}
.product-img {
  width: 100%;
  height: 200px;
  /* margin-left: auto; */
  /* margin-right: auto; */
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}
.clearfix .el-button {
  border-radius: 0;
  border: 0;
}
.product-add {
  width: 200px;
  height: 200px;
  line-height: 200px;
  font-size: 60px;
}
.product-add:hover {
  cursor: pointer;
}
.product-picture-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 200px;
  height: 200px;
  margin-left: auto;
  margin-right: auto;
}
.product-picture-uploader:hover {
  border-color: #409eff;
}
.product-picture-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
}
.product-picture {
  width: 200px;
  height: 200px;
  display: block;
}
</style>